import global from '@/utils/global.js';

export function request(config, success, fail) {
	const headers = {}
	headers['Content-Type'] = config.ContentType || 'application/json;charset=utf-8';
	headers['Authorization'] = uni.getStorageSync('token');
	if (config.loading != false) {
		uni.showLoading({})
	}
	headers['AuthCode'] = global.AuthCode;
	uni.request({
		url: '/cishan-api' + config.url,
		method: config.method,
		params: config.params,
		data: config.params,
		header: headers
	}).then(response => {
		uni.hideLoading()     
		const object = response[1]
        console.log(object)
		if (object.statusCode == 200) {
			const data = object.data
			if (data.code == '00000') {
				if (data.total) {
					success && success(data.data, data.total)
				}else {
					success && success(data.data)
				}
			}else if (data.code == 'A0230' || data.code == 'A0201') {
				uni.removeStorage({ key: 'token' })
				uni.navigateTo({
					url: '/pages/login/index'
				})
			}else {
				uni.showToast({
					title: data.msg || data.error,
					icon: 'none'
				})
				fail && fail(object)
			}
	    }else {
			uni.showToast({
				title: object.error || '请求失败',
				icon: 'none'
			})
			fail && fail(object)
		}
	})
}